<?php

/**
 * news actions.
 *
 * @package    sf_sandbox
 * @subpackage news
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class newsActions extends autonewsActions
{
    
    // to retrieve only this application news
    protected function getNewsOrCreate($id = 'id')
    {
        if (!$this->getRequestParameter($id))
        {
          $news = new News();
          $news->setProjectId(MyConstants::getAppId());
        }
        else
        {
          $news = NewsPeer::retrieveByPk($this->getRequestParameter($id));
          if( $news && ($news->getProjectId() != MyConstants::getAppId()) )
          {
              $news = null;
          }
          $this->forward404Unless($news);
        }
        $news->setCulture(sfContext::getInstance()->getUser()->getCulture());
        return $news;
    }
    // to retrieve only this application partners
    protected function addFiltersCriteria($c)
    {
        /* @var $c Criteria */
        parent::addFiltersCriteria($c);
        $c->add(NewsPeer::PROJECT_ID, MyConstants::getAppId());
    }
    public function executeMoveup()
    {
        $id = (int)$this->getRequestParameter('id', 0);
        $new = NewPeer::retrieveByPK($id);
        $this->forward404Unless($new);
        if( !$id )
        {
            $this->redirect('news/list');
        }

        $c = new Criteria();
        $c->add(NewsPeer::PROJECT_ID, MyConstants::getAppId());
        APriority::moveUp($c, $id);

        $this->forward('news', 'list');
    }
    public function executeStatus()
    {
        $id = (int)$this->getRequestParameter('id', 0);
        if(  News::changeStatus($id))
        {
            $this->redirect('news/list');
        }else 
        {
            $this->forward404();
        }
    }

    public function executeIndex()
    {
        return $this->forward('news', 'showall');
    }

    /**
    * Deletes image by uname (uname from request)
    */
    public function executeDeleteimg()
    {
        if( $this->getRequestParameter('uname') )
        {
            Files::deleteByUname($this->getRequestParameter('uname'));
        }
        $this->redirect('news/edit?id=' . $this->getRequestParameter('id'));
    }

    protected function saveNews($news)
    {
        /* @var $news News */
        $news->save();
        $fileName = $this->getRequest()->getFileName('news[photo]');
        $filePath = $this->getRequest()->getFilePath('news[photo]');
        //debugvar($filePath . ' -&gt; ' . sfConfig::get('sf_web_dir') . '/' . sfConfig::get('app_images_dir') . '/' . sfConfig::get('app_gallery_dir'));
        if( !empty($filePath) )
        {
            Files::saveTo(
                $fileName
                , $filePath
                , $news->getId()
                , get_user_id()
                , MyConstants::TYPE_NEWS
                , $news->getCulture()
                , $news->getStatusId()
                , $news->getName()
                , ''
            );
        }
    }

    /**
    * User can preview newsletter. It contains $this->header requested from previous action 'Newsletter' and list of newsleads selected from NewsPeer by Ids also requested from previous action
    */
    public function executePreview()
    {
        $this->header = $this->getRequestParameter('header');
        $this->newsLeads = NewsPeer::retrieveByPks($this->getRequestParameter('newsItems'));
    }

    public function executeShowall()
    {
        $c = new Criteria();
        $c->add(NewsPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $c->add(NewsPeer::PROJECT_ID, MyConstants::getAppId());
        $c->addDescendingOrderByColumn(NewsPeer::CREATED_AT);

        $pager = new sfPropelPager('News', News::getNewsOnPage());
        $pager->setCriteria($c);
        $pager->setPage($this->getRequestParameter('page', 1));
        $pager->init();
        $this->pager = $pager;
    }

    public function executeView()
    {
        $this->newsItem = NewsPeer::retrieveByPk($this->getRequestParameter('id'));
        $this->newsItem->setCulture(get_culture());
        $this->forward404Unless($this->newsItem);
        $this->title = $this->newsItem->getName();
        $c = new Criteria();
        $c->add(FilesPeer::OBJ_TYPE, MyConstants::TYPE_NEWS);
        $c->add(FilesPeer::OBJ_ID, $this->newsItem->getId());
        $this->files = FilesPeer::doSelect($c);
    }

}
